package com.yxhl.zoume.core.func.map.presenter.location;

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.specialcar.presenter.ISearchPOIPresenter;
import com.yxhl.zoume.core.specialcar.view.SearchPOIView;
import com.yxhl.zoume.utils.LOG;

import java.util.ArrayList;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/4/19.
 */
public class POISearchPresenter implements IPresenter, ISearchPOIPresenter {

    private static final String TAG = "SpecialSearchPresenter";
    /**
     * 高德地图网络请求成功返回码
     */
    private static final int AMAP_REQUEST_SUCCESS = 1000;

    private SearchPOIView mSearchPOIView;

    @Inject
    public POISearchPresenter() {
    }


    @Override
    public void doSearchPOIByKeyWord(Context context, String keyWord, int currentPage, String searchCity) {
        // 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
        PoiSearch.Query query = new PoiSearch.Query(keyWord, "", searchCity);
        query.setPageSize(SEARCH_PAGE_NUM);
        // 设置查第一页
        query.setPageNum(currentPage);

        PoiSearch poiSearch = new PoiSearch(context, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    @Override
    public void doSearchPOINearBy(Context context, int currentPage, String searchCity, LatLonPoint centerPoint) {

    }


    /**
     * 查询poi 回调
     *
     * @param poiResult
     * @param requestCode
     */
    @Override
    public void onPoiSearched(PoiResult poiResult, int requestCode) {
        LOG.e(TAG, "onPoiSearched poiResult=" + poiResult + " requestCode=" + requestCode);
        if (requestCode == AMAP_REQUEST_SUCCESS) {
            ArrayList<PoiItem> poiItems = poiResult.getPois();
            if (poiItems != null) {
                mSearchPOIView.renderPOISearchResult(poiItems);
            } else {
                mSearchPOIView.renderPOISearchOnFailure();
            }
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int requestCode) {
        LOG.e(TAG, "onPoiItemSearched poiItem=" + poiItem);
    }


    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void attachView(BaseView view) {
        if (view != null) {
            if (view instanceof SearchPOIView) {
                mSearchPOIView = (SearchPOIView) view;
            }
        }
    }
}
